package com.loooooo.pm2024.utils;

import com.google.zxing.BinaryBitmap;
import com.google.zxing.EncodeHintType;
import com.google.zxing.Result;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.Base64;
import java.util.Hashtable;

public class QrCode {

    public static void createQrImage(String content, String format, InputStream logo, OutputStream out, Integer width, Integer height)
            throws IOException, WriterException {
        new QRCodeFactory().creatQrImage(content, format, out, logo, width, height);
    }

    public static String createQrCodeBase64(String content, String format, String logo, Integer width, Integer height) {

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            createQrImage(content, format, logo, out, width, height);
            return Base64.getEncoder().encodeToString(out.toByteArray());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (WriterException e) {
            e.printStackTrace();
        }
        return null;

    }

    public static void createQrImage(String content, String format, String logo, OutputStream out, Integer width, Integer height)
            throws IOException, WriterException {
        if (logo == null || logo.length() == 0) {
            new QRCodeFactory().creatQrImage(content, format, out, null, width, height);
        } else {
            InputStream lg = ChartGraphics.createImage(logo, 20 * logo.length(), 16); // createImagenew
            // ByteArrayInputStream(logo.getBytes());
            createQrImage(content, format, lg, out, width, height);
        }
    }

    public static BufferedImage createQrImage(String content, Integer width, Integer height) throws IOException, WriterException {
        return new QRCodeFactory().creatQrImage(content, width, height);
    }

    public static BufferedImage createQrImage(String content, Integer width, Integer height, Hashtable<EncodeHintType, Object> hints) throws IOException, WriterException {
        return new QRCodeFactory().creatQrImage(content, width, height, hints);
    }

    public static Result readQrCodeImage(BufferedImage readImage) throws Exception {
        QRCodeReader qrCodeReader = new QRCodeReader();
        BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(readImage);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Result decode = qrCodeReader.decode(bitmap);
        System.out.println(decode);
        return decode;
    }

    public static void main(String[] args) {
        try {
            BufferedImage img = ImageIO.read(new File("/temp/2023/05/qrcode.png"));
            Result code = readQrCodeImage(img);
            System.out.println(code.getText());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
